package cn.iocoder.yudao.module.dine.category.controller.admin;

import cn.iocoder.yudao.framework.business.basic.annotation.AdminJsonView;
import cn.iocoder.yudao.framework.business.basic.controller.CrudController;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.ShopSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.dine.category.pojo.dto.DineCategoryQueryDTO;
import cn.iocoder.yudao.module.dine.category.pojo.dto.DineCategorySaveDTO;
import cn.iocoder.yudao.module.dine.category.pojo.vo.DineCategoryVO;
import cn.iocoder.yudao.module.dine.category.service.admin.DineCategoryService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/07/18
 */
@Tag(name = "管理后台 - 菜品分类")
@RestController
@RequestMapping("/dine/category")
public class DineCategoryController implements CrudController<DineCategoryVO, DineCategorySaveDTO, DineCategoryQueryDTO> {
    @Resource
    private DineCategoryService dineCategoryService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin', 'shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<DineCategoryVO> getDetail(Long id) {
        return CommonResult.success(dineCategoryService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin', 'shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<PageResult<DineCategoryVO>> getPage(Integer pageNo, Integer pageSize, DineCategoryQueryDTO param) {
        PageResult<DineCategoryVO> page = dineCategoryService.getPage(pageNo, pageSize, param);
        Set<Long> shopIds = convertSet(page.getList(), DineCategoryVO::getShopId);
        Map<Long, ShopSimpleVO> shops = shopInfoApi.getShopSimpleMap(shopIds);
        page.getList().forEach(vo -> vo.setShop(shops.getOrDefault(vo.getShopId(), ShopSimpleVO.EMPTY)));
        return CommonResult.success(page);
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Long> create(DineCategorySaveDTO param) {
        return CommonResult.success(dineCategoryService.create(param));
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return CommonResult.success(dineCategoryService.delete(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin', 'shop_admin')")
    @Override
    public CommonResult<Boolean> update(DineCategorySaveDTO param) {
        return CommonResult.success(dineCategoryService.update(param));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品分类列表")
    @PermitAll
    public CommonResult<List<DineCategoryVO>> getCategoryList() {
        List<DineCategoryVO> list = dineCategoryService.getList(new DineCategoryQueryDTO());
        return success(list);
    }

}
